package com.tadpole.music.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2019/11/21 0021.
 */

public class DateUtils {
    /**
     * 转换时间日期格式字串为long型
     *
     * @param time 格式为：yyyy-MM-dd HH:mm:ss的时间日期类型
     */
    public static Long convertTimeToLong(String time) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            date = sdf.parse(time);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    public static Long convertDateToLong(String Date) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            date = sdf.parse(Date);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 毫秒转换为string
     *
     * @param time 毫秒
     * @return
     */
    public static String getDate(long time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        Date d = new Date(time);
        return sf.format(d);
    }


    /**
     * 当月有多少天
     *
     * @return
     */
    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取上月有多少天
     */
    public static int getLeftMonthLastDay(int position) {
        //计算下一个月
        Calendar calendar = Calendar.getInstance();
        //获得当前日期往后推1个月 amount 为设置的月份值 +为往后推  +号可以省略  -为往前推
        calendar.add(Calendar.MONTH, position);
        //获得下一个月 因为默认月是从0月开始所以要+1
        Integer month = calendar.get(Calendar.MONTH) + 1;
        //获得下一个月是多少年
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        calendar.set(year, month, 0);
        //获得下一个月有多少天
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * 获取
     */
    public static String getLeftMonth(int position) {
        //计算下一个月
        Calendar calendar = Calendar.getInstance();
        //设置为哪个月
        calendar.add(Calendar.MONTH, position);
        //获得下一个月是多少年
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        return year + "/" + month1 ;
    }

    /**
     * 获取开始时间
     */
    public static String getStartTime(int position) throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        return Mutils.strToStr(year+"-"+month1+"-"+"01" + " 00:00:00");
    }

    /**
     * 获取结束时间
     */
    public static String getEndTime(int position) throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        return Mutils.strToStr(year+"-"+month1+"-"+getLeftMonthLastDay(position)+" 23:59:59");
    }


    /**
     * 获取年
     */
    public static int getYear(int position){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        return year;
    }

    /**
     * 获取月
     */
    public static int getMonth(int position){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        return month1;
    }

    /**
     * 获取当前天的开始时间
     */
    public static String getStartDayTime(int position,int day) throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH)+1;
        String str = Mutils.strToStr(year+"-"+month1+"-"+calendar.get(Calendar.DAY_OF_MONTH) + " 00:00:00");
        return str;
    }

    /**
     * 获取当前天的结束时间
     */
    public static String getEndDayTime(int position,int day) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, position);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int year = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH)+1;

        String str = Mutils.strToStr(year+"-"+month1+"-"+calendar.get(Calendar.DAY_OF_MONTH) + " 23:59:59");
        return str;
    }


    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        Log.d("sunyan","-----------------------"+time.toString());
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }

    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }



    /**
     * 今天是几几年几月
     *
     * @return
     */
    public static String getCurrentYearAndMonth() {
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        int month = a.get(Calendar.MONTH) + 1;
        return year + "/" + month + "";
    }

    /**
     * 今天是几几年几月几日
     *
     * @return
     */
    public static String getCurrentYearAndMonthAndDay() {
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        int month = a.get(Calendar.MONTH) + 1;
        int day = a.get(Calendar.DAY_OF_MONTH);
        return year + "/" + month + "/" + day;
    }


    /**
     * 获取当天
     */
    public static Integer getDay() {
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        int month = a.get(Calendar.MONTH) + 1;
        int day = a.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * 获取当月第一天是星期几
     *
     * @return
     */
    public static int getFirstDayOfMonth() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DAY_OF_MONTH, 1);
        int i = a.get(Calendar.DAY_OF_WEEK);
        return i;
    }

    /**
     * 获取星期级
     */
    public static int getWeek(int position){
        Calendar a = Calendar.getInstance();
        a.add(Calendar.MONTH, position);
        a.set(Calendar.DAY_OF_MONTH, 1);
        int i = a.get(Calendar.DAY_OF_WEEK);
        return i;
    }
}
